package org.jeecg.modules.jxc.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.jxc.sale.entity.SaleList;
import org.jeecg.modules.jxc.sale.entity.SaleListGoods;
import org.jeecg.modules.jxc.sale.mapper.SaleListGoodsMapper;
import org.jeecg.modules.jxc.sale.mapper.SaleListMapper;
import org.jeecg.modules.jxc.sale.service.ISaleListService;
import org.jeecg.modules.jxc.sale.vo.CustomerList;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: tb_sale_list
 * @Author: jeecg-boot
 * @Date:   2021-10-26
 * @Version: V1.0
 */
@Service
public class SaleListServiceImpl extends ServiceImpl<SaleListMapper, SaleList> implements ISaleListService {

	@Autowired
	private SaleListMapper saleListMapper;
	@Autowired
	private SaleListGoodsMapper saleListGoodsMapper;

	@Override
	@Transactional
	public void saveMain(SaleList saleList, List<SaleListGoods> saleListGoodsList) {
		saleListMapper.insert(saleList);
		if(saleListGoodsList!=null && saleListGoodsList.size()>0) {
			for(SaleListGoods entity:saleListGoodsList) {
				//外键设置
				entity.setSaleListId(saleList.getId());
				saleListGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(SaleList saleList,List<SaleListGoods> saleListGoodsList) {
		saleListMapper.updateById(saleList);

		//1.先删除子表数据
		saleListGoodsMapper.deleteByMainId(saleList.getId().toString());

		//2.子表数据重新插入
		if(saleListGoodsList!=null && saleListGoodsList.size()>0) {
			for(SaleListGoods entity:saleListGoodsList) {
				//外键设置
				entity.setSaleListId(saleList.getId());
				saleListGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		saleListGoodsMapper.deleteByMainId(id);
		saleListMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			saleListGoodsMapper.deleteByMainId(id.toString());
			saleListMapper.deleteById(id);
		}
	}

	//修改为多表分页查询
	@Override
	public IPage<Map<String, Object>> pageList(Page<Map<String, Object>> page, SaleList saleList) {
		return saleListMapper.selectPageList(page,saleList);
	}


	@Override
	public List<SaleList> selectByMainId(String mainId) {
		return saleListMapper.selectByMainId(mainId);
	}

	@Override
	public List<CustomerList> selectByCustomerId() {
		return saleListMapper.selectByCustomerId();
	}
}
